package com.aircms.controller.admin;


import com.aircms.common.ServerResponse;
import com.aircms.controller.BaseAdminController;
import com.aircms.pojo.validation.users.LoginValidation;
import com.aircms.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "api/admin")

public class LoginAdminController extends BaseAdminController {
    @Autowired
    private IUsersService iUsersService;

    @PostMapping(value = "login")
    public ServerResponse Login(@Validated LoginValidation loginValidation, BindingResult bindingResult) throws Exception{
        try{
            if(bindingResult.hasErrors()){ // 表单验证
                return ServerResponse.response("",-800,bindingResult.getFieldError().getDefaultMessage());
            }
            return iUsersService.login(loginValidation.getName(),loginValidation.getPassword());
        }catch (Exception e){
            this.errorLog(this.getClass(),e);
            throw new Exception("登录失败");
        }
    }
}
